package com.lcc.lynx.base.system.controller;

import com.lcc.lynx.base.system.util.CacheUtil;
import com.lcc.lynx.common.IDict;
import com.lcc.lynx.common.ResultVO;
import com.lcc.lynx.common.cache.GroupCache;
import com.lcc.lynx.common.dict.IDictService;
import com.lcc.lynx.jdbc.IByIdController;
import com.lcc.lynx.base.system.pojo.dv.SysDictDV;
import com.lcc.lynx.base.system.pojo.dv.SysDictDtlDV;
import com.lcc.lynx.base.system.service.SysDictService;
import com.lcc.lynx.common.utils.StrUtil;
import com.lcc.lynx.security.annotation.Anonymous;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 字典表控制实现
 * @auth lcc
 * @date 2025-01-01
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys/dict")
public class SysDictController implements IByIdController<SysDictDV> {

    private SysDictService service;
    private List<IDictService<SysDictDV>> dictBackServiceList;

    @Override
    public SysDictService getService() {
       return service;
    }

    @Anonymous
    @GetMapping("byCode")
    public ResultVO<List<? extends IDict>> byCode(SysDictDV dto){
        if(StrUtil.isEmpty(dto.getDictCode())){
            return ResultVO.error("字典code不能为空");
        }
        for (IDictService<SysDictDV> dictService : dictBackServiceList) {
            if(dictService.eq(dto.getDictCode())){
                return ResultVO.success(dictService.execute(dto));
            }
        }
        List<SysDictDtlDV> dict = CacheUtil.getDict(dto.getDictCode());
        if(dict != null){
            return ResultVO.success(dict);
        }
        return ResultVO.error("字典不存在：" + dto.getDictCode());
    }

}